//
//  MainTabbar.swift
//  SinaWeibo
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit


class MainTabbar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
         addSubview(composeBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
       super.init(coder:aDecoder)
        addSubview(composeBtn)
    }
   //重新布局子控件调用者个方法
    override func layoutSubviews() {
         super.layoutSubviews()
        let w = self.bounds.width*0.2
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        var index:CGFloat = 0
        for subview in subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
              //修改 frame 水平位移
                subview.frame = CGRectOffset(rect, w * index, 0)
                index += (index == 1 ? 2:1)
            }
            
        }
         //设置 加号按钮的位置
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
       
        
    }
   //懒加载 加号按钮
    lazy var composeBtn:UIButton = {
      let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
       btn.sizeToFit()
        return btn
        
    }()
    
    
    
    }
    

